/**
 * Package     response
 * @author     : wgy
 * @time       : 2024/6/4 星期二 上午 11:04
 * explain     ：
**/

package response

import (
	"github.com/zeromicro/go-zero/rest/httpx"
	"net/http"
)

type Resp struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

// Response http返回
func Response(r *http.Request, w http.ResponseWriter, resp interface{}, err error) {
	if err == nil {
		//成功返回
		r := &Resp{
			Code:    200,
			Message: "成功",
			Data:    resp,
		}
		httpx.WriteJson(w, http.StatusOK, r)
		return
	}
	//错误返回
	errCode := int(10086)
	// 可以根据错误码，返回具体错误信息
	errMsg := "服务器错误"

	httpx.WriteJson(w, http.StatusBadRequest, &Resp{
		Code:    errCode,
		Message: errMsg,
		Data:    nil,
	})

}
